package com.wzb.mybatisplus.bitaction.electrumaction.three;

import com.wzb.mybatisplus.bitaction.BitcoinPrivateKeyGenerator1;
import com.wzb.mybatisplus.bitaction.electrumaction.one.BitcoinKeyConverter;
import com.wzb.mybatisplus.bitaction.electrumaction.one.BlockchainBalanceChecker;
import com.wzb.mybatisplus.bitaction.electrumaction.one.KeyPair;
import com.wzb.mybatisplus.entity.Bituse;
import org.json.JSONObject;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.*;

public class SendEmailToQQ {
    private static String from = "3430256920@qq.com"; // 替换为你的QQ邮箱
    private static String password = "rgpoquhgapjocigj"; // 替换为你的SMTP授权码


    private static String from1 = "2267457055@qq.com"; // 替换为你的QQ邮箱
    private static String password1 = "zqfouhqwwpdydige";
    public void sendEmail(String enamilAddress ,String sy){



        String host = "smtp.qq.com";

        // 设置属性
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host); // 设置SMTP服务器
        properties.setProperty("mail.smtp.port", "587"); // 设置端口
        properties.setProperty("mail.smtp.auth", "true"); // 需要认证
        properties.setProperty("mail.smtp.starttls.enable", "true"); // 启用TLS加密
        // 获取默认的Session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password); // 设置发件人邮箱和授权码
            }
        });

        try {
            // 创建MimeMessage对象
            MimeMessage message = new MimeMessage(session);
            // 设置发件人
            message.setFrom(new InternetAddress(from));
            // 设置收件人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(enamilAddress));
            // 设置邮件主题
            message.setSubject("这是一封测试邮件");
            // 设置邮件正文
            message.setText(sy);

            // 发送邮件
            Transport.send(message);
            System.out.println("邮件发送成功！");
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("邮件发送失败：" + e.getMessage());
        }



    }
    public void sendEmail(String enamilAddress ,String sy,int flag){

        String sendQQ = "";
        String passwordQQ = "";
        if(flag==0){
            sendQQ = from;
            passwordQQ = password;
        }else if(flag==1){
            sendQQ = from1;
            passwordQQ = password1;
        }

        String host = "smtp.qq.com";

        // 设置属性
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host); // 设置SMTP服务器
        properties.setProperty("mail.smtp.port", "587"); // 设置端口
        properties.setProperty("mail.smtp.auth", "true"); // 需要认证
        properties.setProperty("mail.smtp.starttls.enable", "true"); // 启用TLS加密

        // 获取默认的Session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                String sendQQ = "";
                String passwordQQ = "";
                if(flag==0){
                    sendQQ = from;
                    passwordQQ = password;
                }else if(flag==1){
                    sendQQ = from1;
                    passwordQQ = password1;
                }
                return new PasswordAuthentication(sendQQ, passwordQQ); // 设置发件人邮箱和授权码
            }
        });

        try {
            // 创建MimeMessage对象
            MimeMessage message = new MimeMessage(session);
            // 设置发件人
            message.setFrom(new InternetAddress(sendQQ));
            // 设置收件人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(enamilAddress));
            // 设置邮件主题
            message.setSubject("这是一封测试邮件");
            // 设置邮件正文
            message.setText(sy);

            // 发送邮件
            Transport.send(message);
            System.out.println("邮件发送成功！");
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("邮件发送失败：" + e.getMessage());
        }



    }

    public static void main(String[] args) {
        // 发件人邮箱和授权码
        String from = "your_email@qq.com"; // 替换为你的QQ邮箱
        String password = "your_authorization_code"; // 替换为你的SMTP授权码

        SendEmailToQQ sendEmailToQQ = new SendEmailToQQ();
        sendEmailToQQ.sendEmail("3430256920@qq.com","hhhhhh",1);
//        Set<String> addresses = new HashSet<>();
//        Set<String> addressesSys = new HashSet<>();
//        for (int i = 0; i <= 10000000; i++) {
//
//            BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey = BitcoinPrivateKeyGenerator1.generatePrivateKey();
//            if (BitcoinPrivateKeyGenerator1.isValidPrivateKey(privateKey.getPrivateKey())) {  //私钥正确的话
//
//                // 私钥转公钥
//                String wifPrivateKey = privateKey.getWIFCompressed();
//
//                KeyPair wifKeyPair = null;
//                try {
//                    wifKeyPair = BitcoinKeyConverter.generateFromWIF(wifPrivateKey);
//                } catch (Exception e) {
//
//                }
//                String address = wifKeyPair.getLegacyAddress(); // 比特币创始区块地址公钥地址
//                addresses.add(address);
//                addressesSys.add(wifPrivateKey);
//            }
//            if (addresses.size() >= 300) {
//                List<String> list = new ArrayList<>();
//                list.addAll(addresses);
//                // 批量调用比特币
//                String result = BlockchainBalanceChecker.getBalanceForAddresses(list);
//                for(;result==null;){
////                    Thread.sleep(1000);
////                    flag++;
////                    System.out.println("=======调用比特币地址返回空，走进死循环中了======="+flag);
//                    result = BlockchainBalanceChecker.getBalanceForAddresses(list);
//                }
////                String addressString = String.join("|", addresses);
////                System.out.println(addressString);
////                System.out.println("============");
////                System.out.println(addressesSys);
//                JSONObject jsonResponse = new JSONObject(result);
//
//                for (String address : addresses) {
//                    JSONObject addressInfo = jsonResponse.getJSONObject(address);
////                    System.out.println(address+":    "+addressInfo);
//                    Long maney = addressInfo.getLong("final_balance");
//                    long total_received = addressInfo.getLong("total_received");
//                    long jiaoyiliang = addressInfo.getLong("n_tx");
//                    if (maney > 0 || jiaoyiliang > 0 || total_received > 0) {
//                        System.out.println("=======金额======" + maney + "私钥：" + addressesSys);
////                        conutsum++;
//                        Bituse bituse = new Bituse();
//                        List<String> listSys = new ArrayList<>();
//                        listSys.addAll(addressesSys);
////                        bituse.setWifsy(listSys);
////                        bituse.setMask(maney + "");
////                        bituseMapper.insert(bituse);
//                        if (maney > 0) {
//                            SendEmailToQQ sendEmailToQQ = new SendEmailToQQ();
//                            sendEmailToQQ.sendEmail("2267457055@qq.com", "金额："+maney+"   "+ addressesSys.toString());
//
////                            for(String s : addressesSys){
////                                fileUtils.writeFile( s+"\n",path);
////                            }
//                            System.out.println();
//
//                        }
//
//                    }
//
//
//                }
//                addresses.clear();
//                addressesSys.clear();
//            }
//        }
    }
}
